package com.simplegovern.authority.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.opensymphony.xwork2.Preparable;
import com.simpleframe.domain.state.StateContext;
import com.simpleframe.domain.state.StateVo;
import com.simpleframe.security.AuthorityContext;
import com.simpleframe.security.Role;
import com.simpleframe.web.action.CrudActionSupport;
import com.simplegovern.authority.SysGroup;
import com.simplegovern.authority.SysUser;
import com.simplegovern.authority.service.GroupService;
import com.simplegovern.authority.service.RoleService;
import com.simplegovern.authority.service.UserRoleService;
import com.simplegovern.authority.service.UserService;

@Scope("prototype")
@Namespace("/user")
@Action("/user/user")
public class UserAction extends CrudActionSupport  implements Preparable{
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private GroupService groupService;
	private SysUser user;
	private List<Role> roles;
	private Long[] roleIds;

	@Override
	public String main(){
		return MAIN;
	}
	
	@Override
	public String grid() throws Exception {
		page = userService.getSysUserPage(getSearcher());
		return GRID;
	}

	public String create() throws Exception {
		setSaveMode();
		user = new SysUser();
		user.setState(1);//正常
		roles = AuthorityContext.getRoles();
		return EDIT;
	}
	
	@Override
	public String edit() throws Exception {
		setUpdateMode();
		user = userService.getSysUser(user.getId());
		roles = AuthorityContext.getRoles(userRoleService.getRolesByUserId(user.getId()));
		return EDIT;
	}
	
	@Override
	public String save() throws Exception {
		user.setState(1);//设置默认值
		user.setInsertTime(new Date());
		userService.createSysUser(user,roleIds);
		addMessage("用户添加成功");
		return null;
	}
	
	public String update() throws Exception {
		//密码为空时不修改密码
		if("".equals(user.getPassword())){
			SysUser up = userService.getSysUser(user.getId());
			user.setPassword(up.getPassword());
		}
		userService.updateSysUser(user,roleIds);
		addMessage("用户修改成功");
		return null;
	}
	
	public void prepareUpdate() throws Exception {
		user = userService.getSysUser(getId());
	}
	
	public void validator() throws Exception {
		System.out.println("执行验证");
	}
	
	@Override
	public String delete() throws Exception {
		userService.deleteSysUser(ids);
		addMessage("用户删除成功");
		return null;
	}
	
	public List<StateVo> getUserStateList(){
		return StateContext.getItemsList("userState");
	}
	
	public List<SysGroup> getGroupList(){
		return groupService.getGroups();
	}
	
	//setters and getters
	
	public void setUser(SysUser user) {
		this.user = user;
	}
	
	public SysUser getUser() {
		return user;
	}
	
	public List<Role> getRoles() {
		return roles;
	}
	
	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}
}
